package com.time.workbench.core.util;


import com.time.workbench.auth.entity.base.ActiveUser;
import com.time.workbench.core.consts.RequestAttributeKeyConst;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class AuthUtil {

    private final static ThreadLocal<Map<String, Object>> holder = new ThreadLocal<>();

    /**
     * 获取当前用户
     */
    public static ActiveUser getCurrUser() {
        HttpServletRequest request = ContextUtil.getRequest();
        if (request == null) {
            return null;
        }
        ActiveUser competenceUser = (ActiveUser) request.getAttribute(RequestAttributeKeyConst.USERINFO);
        return competenceUser;
    }


    /**
     * 设置租户id
     */
    public static void setRequestTenantId(Integer tenantId) {
        if (RequestContextHolder.getRequestAttributes() != null) {
            HttpServletRequest request = ContextUtil.getRequest();
            request.setAttribute(RequestAttributeKeyConst.tenantId, tenantId);
        }
        setTenantId(tenantId);
    }

    public static void setTenantId(Object value) {
        getContext().put(RequestAttributeKeyConst.tenantId, value);
    }

    public static Integer getTenantId() {
        Integer tenantId = (Integer) getContext().get(RequestAttributeKeyConst.tenantId);
        if (tenantId == null)
            return 1;
        return tenantId;
    }

    private static Map<String, Object> getContext() {
        Map map = holder.get();
        if (map == null) {
            map = new HashMap();
            holder.set(map);
        }
        return map;
    }

    /**
     * 获取当前租户id
     */
    public static Integer getCurrTenantId() {
        HttpServletRequest request = ContextUtil.getRequest();
        if (request == null) {
            return 1;
        }
        Integer tenantId = (Integer) request.getAttribute(RequestAttributeKeyConst.tenantId);
        if (tenantId == null)
            return getTenantId();
        return tenantId;
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static Integer getCurrUserId() {
        ActiveUser currUser = getCurrUser();
        if (currUser == null)
            return null;
        return currUser.getActiveUsers().getId();
    }


    /**
     * 获取当前用户登录名
     *
     * @return
     */
    public static String getCurrUserLoginName() {
        ActiveUser currUser = getCurrUser();
        if (currUser == null)
            return null;
        return currUser.getActiveUsers().getLoginName();
    }

    /**
     * 获取当前用户昵称
     *
     * @return
     */
    public static String getCurrUserNickName() {
        ActiveUser currUser = getCurrUser();
        if (currUser == null)
            return null;
        return currUser.getActiveUsers().getNickName();
    }

    public static String getToken() {
        HttpServletRequest request = ContextUtil.getRequest();
        return getToken(request);
    }

    public static String getToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (token == null)
            token = request.getHeader("token");
        return token;
    }


}
